package com.iweb.web.servlet;

import com.alibaba.druid.pool.DruidDataSource;
import com.iweb.commons.utils.UrlUtil;
import com.iweb.dao.EmpDao;
import com.iweb.dao.MerchantDao;
import com.iweb.dao.impl.EmpDapImpl;
import com.iweb.dao.impl.FoodDaoImpl;
import com.iweb.dao.impl.MerchantDaoImpl;
import com.iweb.service.EmpService;
import com.iweb.service.impl.EmpServiceImpl;
import com.iweb.service.impl.FoodServiceImpl;
import com.iweb.service.impl.MerchantServiceImpl;
import com.iweb.web.controller.EmpController;
import com.iweb.web.controller.FoodController;
import com.iweb.web.controller.MerchantController;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: jack
 * @date: 2022-08-04 0004
 * @ApiNOTE:
 */
@WebServlet(urlPatterns = "/static/*", loadOnStartup = 1)
public class BaseServlet extends HttpServlet {

  private Map<String, String> staticRequest = new HashMap<>();

  @Override
  public void init() throws ServletException {


    staticRequest.put("home", "/WEB-INF/views/main.jsp");
    staticRequest.put("pwd", "/WEB-INF/views/emp/pwd.jsp");
    staticRequest.put("menuList", "/WEB-INF/views/food/index.jsp");

    ServletContext application = getServletContext();
    // 初始化所有的 Controller
    EmpController empController = new EmpController();
    application.setAttribute("emp", empController);

    FoodController foodController = new FoodController();
    application.setAttribute("food", foodController);

    MerchantController merchantController = new MerchantController();
    application.setAttribute("mer", merchantController);

    // 初始化 service
    EmpServiceImpl empService = new EmpServiceImpl();
    FoodServiceImpl foodService = new FoodServiceImpl();
    MerchantServiceImpl merchantService = new MerchantServiceImpl();

    // 初始化 所有 dao
    EmpDapImpl empDao = new EmpDapImpl();
    FoodDaoImpl foodDao = new FoodDaoImpl();
    MerchantDaoImpl merchantDao = new MerchantDaoImpl();

    // 初始化成员变量
    empService.setEmpDao(empDao);
    empController.setEmpService(empService);

    foodController.setFoodService(foodService);
    foodService.setFoodDao(foodDao);

    merchantController.setMerchantService(merchantService);
    merchantService.setMerchantDao(merchantDao);
    merchantService.setEmpDao(empDao);


    // 初始化数据库连接池
    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mysql://localhost:3306/web?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull");
    dataSource.setUsername("root");
    dataSource.setPassword("123456");
    dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");

    empDao.setDataSource(dataSource);
    foodDao.setDataSource(dataSource);
    merchantDao.setDataSource(dataSource);

    try {
      Connection conn = dataSource.getConnection();
      conn.close(); // 不是真的关闭数据库, 而是将 Connection 返回给连接池
    } catch (SQLException e) {
      e.printStackTrace();
    }

  }

  @Override
  protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    // 处理静态请求
    String action = UrlUtil.action(req, "/static/");
    req.getRequestDispatcher(staticRequest.get(action)).forward(req, resp);
  }
}
